/**
 * Created by diracsun on 2017/02/15.
 *
 */
var commonctl = (function ($, commonser, g) {
    var ret = {};

    var calctpl = [
        '<div >',
        '<div class="pure-g  padding-left padding-right-lg padding-vertical-xs text-lg wt-news-list-bb">',
        '<div class="pure-u-1-2"><i class="iconfont icon-58 text-primary  "></i> 贷款计算器</div>',
        '<div class="pure-u-1-2 text-right"><i class="iconfont icon-guanbi text-666 cur" id="closecalc"></i></div>',
        '</div>',
        '<form class="pure-form padding-vertical-sm padding-horizontal-xs wt-dk-clac">',
        '<div class="pure-g padding-horizontal padding-bottom padding-top-xs">',
        '<div class="pure-u-5-24 text-right padding-top-xs padding-right-xs strong text-sm">贷款金额:</div>',
        '<div class="pure-u-19-24" style="position: relative">',
        '<input  type="text"  class="pure-input-1 text-sm wt-input-dkje" name="dkje" maxlength="8" placeholder="贷款金额" required> ',
        '<span class="text-sm  wt-calc-input-lable text-danger">万元</span></div>',
        '</div>',
        '<div class="pure-g padding-horizontal padding-bottom">',
        '<div class="pure-u-5-24 text-right padding-top-xs padding-right-xs strong text-sm">贷款期限:</div>',
        '<div class="pure-u-19-24">',
        '<select data-placeholder="贷款期限"  name="dkqx" class="chosen-select text-sm padding-left-sm" required>',
        '<option value="0"></option>',
        // '<option value="1">1个月</option>',
        // '<option value="2">2个月</option>',
        '<option value="3">3个月</option>',
        // '<option value="4">4个月</option>',
        // '<option value="5">5个月</option>',
        '<option value="6">6个月</option>',
        // '<option value="7">7个月</option>',
        // '<option value="8">8个月</option>',
        '<option value="9">9个月</option>',
        // '<option value="10">10个月</option>',
        // '<option value="11">11个月</option>',
        '<option value="12">12个月</option>',
        '<option value="24">24个月</option>',
        '<option value=36"">36个月</option>',
        // '<option value="60">60个月</option>',
        '</select></div>',
        '</div>',
        '<div class="pure-g padding-horizontal padding-bottom">',
        '<div class="pure-u-5-24 text-right padding-top-xs padding-right-xs strong text-sm">年利率:</div>',
        '<div class="pure-u-19-24" style="position: relative">',
        '<input  type="text" class="pure-input-1 text-sm wt-input-nll" name="nll"   maxlength="5" placeholder="年利率" required>',
        '<span class="text-sm wt-calc-input-lable text-danger">%</span>',
        '</div>',
        '</div>',
        '<div class="pure-g padding-horizontal padding-bottom">',
        '<div class="pure-u-5-24 text-right padding-top-xs padding-right-xs strong text-sm">还款方式:</div>',
        '<div class="pure-u-19-24">',
        '<select data-placeholder="还款方式" name="hkfs"  id="hkfs" class="chosen-select text-sm padding-left-sm" required>',
        '<option value="0"></option>',
        '<option value="h1">等额本息还款</option>',
        '<option value="h2">按月等本等息</option>',

        // '<option value="h3">到期兑付</option>',
        // '<option value="h4">跨栏还款</option>',
        // '<option value="h5">按月付息按年还款</option>',
        // '<option value="h6">到期一次性还本付息</option>',
        '<option value="h7">每月还息到期还本</option>',
        '</select></div>',
        '</div>',
        '<div class="padding-horizontal"><input type="button" style="width: 100%" class="text-lg pure-button pure-button-primary wt-dk-clac-sub" value="开始计算"></div>',
        '<div class="pure-g text-sm padding-horizontal-xl padding-top-sm wt-news-list-bb"></div>',
        '<div class="pure-g text-sm padding-horizontal-xs padding-top-xs" style="line-height: 28px;">',
        '<div class="pure-u-1-4">月供:</div>',
        '<div class="pure-u-3-4 text-right "><span class="padding-right-xs wt-result-yg text-danger">0.00</span>元</div>',
        '</div>',

        '<div class="pure-g text-sm padding-horizontal-xs" id="showlast" style="line-height: 28px;display: none;">',
        '<div class="pure-u-1-4">最后一期本息:</div>',
        '<div class="pure-u-3-4 text-right "><span class="padding-right-xs wt-result-last text-danger">0.00</span>元</div>',
        '</div>',


        '<div class="pure-g text-sm padding-horizontal-xs" style="line-height: 28px;">',
        '<div class="pure-u-1-4">还款月数:</div>',
        '<div class="pure-u-3-4 text-right"><span class="padding-right-xs wt-result-hkys text-danger">0</span>月</div>',
        '</div>',
        '<div class="pure-g text-sm padding-horizontal-xs" style="line-height: 28px;">',
        '<div class="pure-u-1-4">总利息:</div>',
        '<div class="pure-u-3-4 text-right"><span class="padding-right-xs wt-result-zlx text-danger">0.00</span>元</div>',
        '</div>',
        '<div class="pure-g text-sm padding-horizontal-xs" style="line-height: 28px;">',
        '<div class="pure-u-1-4">本息合计:</div>',
        '<div class="pure-u-3-4 text-right"><span class="padding-right-xs wt-result-bxhj text-danger">0.00</span>元</div>',
        '</div>',
        '</form>',
        '</div>',
    ].join("")

    function isPlaceholer() {
        var input = document.createElement('input');
        return "placeholder" in input;
    }

    $(document).on("click", ".wt-f-calc", function () {

        layer.open({
            type: 1
            , title: false
            , id: 'calcdiv'
            , area: ['438px', '506px']
            , content: calctpl
            , shade: false
            , offset: 'b'
            , resize: false
            , success: function (layero, index) {
                layer.style(index, {
                    marginLeft: 380
                });
            }
        });

        $('.chosen-select').chosen({
            disable_search: true,
            width: '100%'
        })
        if (!isPlaceholer()) {
            $('input, textarea').placeholder({
                customClass: 'ie8-placeholder'
            });
        }
        return false

    })

    $(document).on("click", "#closecalc", function () {
        layer.closeAll('page');
    })

    $(document).on("mouseover mouseout", '.wt-bt-con', function (event) {
        if (event.type == "mouseover") {
            $(this).find(".wt-bt-btn-p").removeClass("hidden");
        } else if (event.type == "mouseout") {
            $(this).find(".wt-bt-btn-p").addClass("hidden");

        }
    });

    $(document).on("click", ".wt-dk-clac-sub", function () {
        var clacdata = $(".wt-dk-clac").form2Json(),
            dkje = parseFloat(clacdata.dkje),        //贷款金额
            nll = parseFloat(clacdata.nll),           //年利率
            yll = nll / 100 / 12,  //月利率
            dkqx = parseInt(clacdata.dkqx),
            money = dkje * 10000,
            lixi = 0.00,
            heji = 0.00,
            yh = 0.00;
        if (dkje == 0.00) {
            layer.alert("贷款金额不能为空！");
            return false;
        }

        if (dkqx === 0) {
            layer.alert("贷款期限不能为空！");
            return false;
        }

        if (nll == 0.00) {
            layer.alert("年利率不能为空！");
            return false;
        }

        if (clacdata.hkfs == 'h1') {
            yh = money * yll * (Math.pow(1 + yll, dkqx)) / (Math.pow(1 + yll, dkqx) - 1);
            heji = yh * dkqx;
            lixi = heji - money;

        } else if (clacdata.hkfs == 'h2') {
            lixi = dkqx * (money * yll - yll * (money / dkqx) * (dkqx - 1) / 2 + money / dkqx) - money;
            heji = lixi + money;
            yh = money / dkqx + money * yll;
        }
        // else if (clacdata.hkfs == 'h3') {
        //
        // }
        /*  else if (clacdata.hkfs == 'h4') {
              if (dkqx ==36) {
                  alert("跨栏还款月付息,限期36个月！");
                  return false;
              }
              else if((dkqx>0&&dkqx<12)||(dkqx>12&&dkqx<24)||(dkqx>22&&dkqx<36))
              {


              }
              else if(dkqx===12)
              {

              }
              else if(dkqx===24)
              {

              }
              else if(dkqx===36)
              {

              }
              else {
                  return false;
              }

          }
          else if (clacdata.hkfs == 'h5') {

          }
          else if (clacdata.hkfs == 'h6') {

          }
         */
        else if (clacdata.hkfs == 'h7') {
            yh = money * yll;
            lixi = yh * dkqx;
            heji = yh * dkqx + money;
            var yhlast = money + yh;
            $(".wt-result-last").text(yhlast.toFixed(2));
            $("#showlast").show();
        }
        else {
            return;
        }

        $(".wt-result-yg").text(yh.toFixed(2));
        $(".wt-result-zlx").text(lixi.toFixed(2));
        $(".wt-result-hkys").text(dkqx);
        $(".wt-result-bxhj").text(heji.toFixed(2));
    })


    $(document).on("change blur", "#hkfs", function () {
        $("#showlast").hide();
    });

    $(document).on("keyup blur", ".wt-input-nll", function () {
        $(this).val($(this).val().replace(/[^\d.]/g, '').replace(/^\./g, '').replace(/\.{2,}/g, '').replace('.', "$#$").replace(/\./g, '').replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'));
    });

    $(document).on("keyup blur", ".wt-input-dkjj", function () {
        $(this).val($(this).val().replace(/[^0-9-]+/, ''));
    });

    $(document).on("click", ".wt-prod-href", function () {
        var prodid = $(this).data("prodid");
        g.go("/product.html?prodid=" + prodid)
    });

    $(document).on("click", ".wt-news-href", function () {
        var newsid = $(this).data("newsid");
        g.go("/news.html?newsid=" + newsid, 1)
    });

    $(document).on("click", ".wt-qa-href", function () {
        var qaid = $(this).data("qaid");
        g.go("/qa.html?qaid=" + qaid, 1)
    });


    $(document).on("click", ".wt-readme-href", function () {
        var readmeid = $(this).data("readmeid");
        g.go("/readme.html?readmeid=" + readmeid, 1)
    });

    $(document).on("click", ".wt-notify-href", function () {
        var notifyid = $(this).data("notifyid");
        g.go("/notify.html?notifyid=" + notifyid, 1)
    });

    $(document).on("click", "#wt-notify-div", function () {
        g.go("/notifylist.html", 1)
    });

    $(document).on("click", ".wt-captcha-img", function () {
        $(this).attr("src", "/login/api/captchaImage/144/31/loginCaptchaImage/0?v=" + g.getTs())
    })

    $(document).on("click", ".wt-menu-logout-btn", function () {
        ret.logout({});
    })


    $(document).on("click", "#wt-reg-xy", function () {

       $(this).is(':checked')?$(".wt-submit-btn").removeClass("pure-button-disabled"): $(".wt-submit-btn").addClass("pure-button-disabled")



    })


    $(document).on("click", ".wt-login-tab", function () {
        $(".wt-login-tab").addClass("text-primary").addClass("wt-bge");
        $(this).removeClass("text-primary").removeClass("wt-bge");
        var formclass= $(this).data("class");
        if(formclass=='wt-reg-form')
        {
                $("input[type!='button']").val("");
                $("#wt-reg-xy").attr("checked",!1);
                $(".wt-send-sms-btn").addClass("pure-button-disabled").val("发送验证码");
                $(".wt-submit-btn").addClass("pure-button-disabled")
                vregf.resetForm();
        }
        else {
            $("input[type='submit']").val("登录");
        }
        $(".pure-form").addClass("hidden");
        $("."+formclass).removeClass("hidden");
    });

   
  var vregf=  $(".wt-reg-form").validate({
        rules: {
            realName:{
                zhcnrule: true
            },
            userName:{
                strrule: true,
            },
            mobile:{
                mobilerule:true,
            },
            password: {
                chrnumrule:true
            },
            cpassword: {
                chrnumrule:true,
                equalTo: "#password"
            }
        },
        messages: {
            realName:{
                required: "请输入姓名",
                minlength: "姓名不能少于2个汉字",
                maxlength: "姓名不能少于2个汉字"

            },

            userName:{
                required: "请输入用户名",
                minlength: "用户名不能少于6个字符",
                maxlength: "用户名不能多于16个字符"
            },
            mobile:{
                required:"请输入手机号"
            },
            password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字母"
            },
            cpassword: {
                required: "请输入确认密码",
                minlength: "密码长度不能小于 6 个字母",
                equalTo: "两次密码输入不一致"
            },
            webCaptcha:{
                required:"请输入图片验证码"
            },
            smsCode:{
                required:"请输入短信验证码"
            }

        },
        errorPlacement:function(error,element) {
            $(element).closest("form").find("div[for='"+element.attr("id")+"']").append(error)


        },
        submitHandler:function(form) {
            var regdata = $(".wt-reg-form").form2Json();
            console.log(JSON.stringify(regdata));
            commonctl.reg(regdata);
        }
    });


   $(".wt-login-form").validate({
        rules: {
            webAccount:{
                mobilerule: true
            },
            webPassword: {
                chrnumrule:true
            }
        },
        messages: {
            webAccount:{
                required:"请输入手机号"
            },
            webPassword: {
                required: "请输入密码",
                minlength: "密码长度不能小于6个字母"
            },
            loginCaptcha:{
                required:"请输入图片验证码",
                minlength: "请输入四位图片验证码"
            }

        },
        errorPlacement:function(error,element) {
            $(element).closest("form").find("div[for='"+element.attr("id")+"']").append(error)
        },
        submitHandler:function() {
            var logindata = $(".wt-login-form").form2Json();
            logindata.webCaptcha=logindata.loginCaptcha;
            // logindata.webPassword=$.md5(logindata.webPassword);
            delete logindata.loginCaptcha;
            ret.login(logindata);
        }
    });
    
    //监听注册时 手机号 验证成功失败
    $('.mobile,.webCaptcha').on('input', function(e) {
        if($(".wt-reg-form").validate().element($(".mobile"))&&$(".wt-reg-form").validate().element($(".webCaptcha"))){

            $(".wt-send-sms-btn").removeClass("pure-button-disabled").val("发送验证码");
        } else {

            $(".wt-send-sms-btn").addClass("pure-button-disabled").val("发送验证码");
        }
    });

    //发送验证码 验证新手机
    $(".wt-send-sms-btn").on('click', function() {
        var validCode=true;
        var time=60;
        var mobile= $("#mobile").val();
        var webCaptcha= $("#webCaptcha").val();
        var code=$(this);
        var data= commonser.sendSms({"mobile":mobile,"webCaptcha":webCaptcha,"ASYNC":false});
        console.log("json" + JSON.stringify(data));

        if(data.responseJSON!= null){
            if (validCode) {
                if(data.responseJSON.error!=null){
                    layer.alert(data.responseJSON.error);
                }
                validCode=false;
                $(".wt-send-sms-btn").attr("disabled", true);
                var t=setInterval(function  () {
                    time--;
                    code.val(time+"秒");
                    if (time==0) {
                        clearInterval(t);
                        code.val("重新获取");
                        validCode=true;
                        $(".wt-send-sms-btn").attr("disabled", false);
                    }
                },1000)
            }
        }
    });


    //------找回密码-----------------------------------
    $(".wt-getBackPwd-form").validate({
        rules: {
            getBackPwd_mobile:{
                mobilerule: true
            },
            getBackPwd_webCaptcha: {
                chrnumrule:true
            }
        },
        messages: {
            getBackPwd_mobile:{
                required:"请输入手机号"
            },
            getBackPwd_webCaptcha:{
                required:"请输入图片验证码",
                minlength: "请输入四位图片验证码"
            }

        },
        errorPlacement:function(error,element) {
            $(element).closest("form").find("div[for='"+element.attr("id")+"']").append(error)
        },
        submitHandler:function() {
            var data = $(".wt-getBackPwd-form").form2Json();
            console.log(data)
            ret.getBackPwd(data);
        }
    });

    /**
     * 新闻列表模板
     * @type {string}
     */
    var newsListTpl = [
        '{@each news as i,k}',
        '<div class="cur wt-news-href wt-news-list-bb" data-newsid="${i.id}">',
        '{@if k==0}',
        '{@if i.img==null}',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.title}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.contentDesc}</div>',
        '<div class="padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '{@else}',
        '<div class="pure-g">',
        '<div class="pure-u-5-6">',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.title}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.contentDesc}</div>',
        '<div class="padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '</div>',
        '<div class="pure-u-1-6 padding-left-xs">',
        '<img src="${i.img}" style="height:132px;width:100%;background:red;">',
        '</div>',
        '</div>',
        '{@/if}',
        '{@else}',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.title}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.contentDesc}</div>',
        '<div class="padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '{@/if}',
        '</div>',
        '{@/each} '
    ].join("");

    /**
     * 新闻模板
     * @type {string}
     */
    var newstpl = [
        '<div class="text-center margin-top-xxl">',
        '<h2 class="center">${title}</h2>',
        '</div>',
        '<div class="text-center text-sm text-999 padding-top-xs padding-bottom-sm wt-news-list-bb">',
        '发布时间 : ${publishTime | ts2date}',
        '</div>',
        '<div class="text-sm padding-horizontal-sm padding-vertical-sm margin-bottom-xxl" style="min-height:400px;line-height:24px;">',
        '$${content}',
        '</div>'
    ].join("");

    /**
     * 常见问题
     * @type {string}
     */
    var qaListTpl = [
        '{@each news as i}',
        '<div class="cur wt-qa-href wt-news-list-bb" data-qaid="${i.id}">',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.title}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.contentDesc}</div>',
        '<div class=" padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '</div>',
        '{@/each} '
    ].join("");

    /**
     * 新手必读列表模板
     * @type {string}
     */
    var readmeListTpl = [
        '{@each news as i}',
        '<div class="cur wt-readme-href wt-news-list-bb" data-readmeid="${i.id}">',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.title}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.contentDesc}</div>',
        '<div class=" padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '</div>',
        '{@/each} '
    ].join("");

    /**
     * 公告模板
     * @type {string}
     */
    var notifytpl = [
        '<div class="text-center margin-top-xxl">',
        '<h2 class="center">${noticeTitle}</h2>',
        '</div>',
        '<div class="text-center text-sm text-999 padding-top-xs padding-bottom-sm wt-news-list-bb">',
        '发布时间 : ${publishTime | ts2date}',
        '</div>',
        '<div class="text-sm padding-horizontal-sm padding-vertical-sm margin-bottom-xxl" style="min-height:400px;line-height:24px;">',
        '$${noticeContent}',
        '</div>'
    ].join("");

    /**
     * 公告列表模板
     * @type {string}
     */
    var notifiesListTpl = [
        '{@each notifies as i}',
        '<div class="cur wt-notify-href wt-news-list-bb" data-notifyid="${i.id}">',
        '<div class="margin-vertical-sm wt-news-list-li"><div class="text-lg padding-left">${i.noticeTitle}</div></div>',
        '<div class="line-clamp3 padding-left text-sm">$${i.noticeContent}</div>',
        '<div class=" padding-left text-999 padding-top-xs" style="padding-bottom: 4px;">${i.publishTime | ts2date}</div>',
        '</div>',
        '{@/each} '
    ].join("")


    /**
     * 贷款产品查询
     * @param req
     */
    ret.qryNotifyArr = function (req) {
        var req = $.extend({}, {"page": 1, "rows": 6}, req); //默认参数
        $.when(commonser.qryNotifyArr(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-news-list").html(juicer(notifiesListTpl, {"notifies": resdata.rows})).page({
                        pageObj: $.extend({}, {
                            "totalRow": resdata.total,
                            "totalPage": Math.ceil(resdata.total / req.rows)
                        }, req),
                        foo: ret.qryNotifyArr //重复调用分页
                    });
                }
            })
    };

    ret.qryNotifyDetail = function (req) {
        var req = $.extend({}, req); //默认参数
        $.when(commonser.qryNotifyDetail(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-new-detail").html(juicer(notifytpl, resdata));
                }
            })
    };


    ret.qryNewsArr = function (req) {
        var req = $.extend({}, {"page": 1, "rows": 6, "type": "11"}, req); //默认参数
        $.when(commonser.qryContentArr(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-news-list").html(juicer(newsListTpl, {"news": resdata.rows})).page({
                        pageObj: $.extend({}, {
                            "totalRow": resdata.total,
                            "totalPage": Math.ceil(resdata.total / req.rows)
                        }, req),
                        foo: ret.qryNewsArr //重复调用分页
                    });
                }
            })
    };


    ret.qryNewsDetail = function (req) {
        var req = $.extend({}, req); //默认参数
        $.when(commonser.qryContentDetail(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-news-detail").html(juicer(newstpl, resdata));
                }
            })
    };


    ret.qryQaArr = function (req) {
        var req = $.extend({}, {"page": 1, "rows": 6, "type": "12"}, req); //默认参数
        $.when(commonser.qryContentArr(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-qa-list").html(juicer(qaListTpl, {"news": resdata.rows})).page({
                        pageObj: $.extend({}, {
                            "totalRow": resdata.total,
                            "totalPage": Math.ceil(resdata.total / req.rows)
                        }, req),
                        foo: ret.qryQaArr //重复调用分页
                    });
                }
            })
    };

    ret.qryReadmeArr = function (req) {
        var req = $.extend({}, {"page": 1, "rows": 6, "type": "13"}, req); //默认参数
        $.when(commonser.qryContentArr(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-readme-list").html(juicer(readmeListTpl, {"news": resdata.rows})).page({
                        pageObj: $.extend({}, {
                            "totalRow": resdata.total,
                            "totalPage": Math.ceil(resdata.total / req.rows)
                        }, req),
                        foo: ret.qryReadmeArr //重复调用分页
                    });
                }
            })
    };

    var abouttpl = [
        '<div class="text-center margin-top-xxl">',
        '<h2 class="center">${title}</h2>',
        '</div>',
        // '<div style="border-bottom:1px solid #ddd;" class="text-center text-sm text-999 padding-top-xs padding-bottom-sm">',
        // '发布时间:${createTime | ts2date}',
        // '</div>',
        '<div class="text-sm padding-horizontal-sm padding-vertical-sm margin-bottom-xxl margin-top" style="min-height:400px;line-height:24px;">',
        '$${content}',
        '</div>'
    ].join("");


    ret.qryAboutMe = function (req) {
        var req = $.extend({}, req); //默认参数
        $.when(commonser.qryAboutMe(req))
            .done(function (resp) {
                if (resp.success) {
                    var resdata = resp.result;
                    $(".wt-about-detail").html(juicer(abouttpl, resdata));
                }
            })
    };

    ret.reg = function (req) {
        var req = $.extend({}, req); //默认参数
        $.when(commonser.reg(req))
            .done(function (resp) {
                if (resp.success) {
                   g.go("/login.html")

                }
                else
                {
                    $(".wt-login-error").text(resp.error);
                    $(".wt-captcha-img").attr("src", "/login/api/captchaImage/144/31/loginCaptchaImage/0?v=" + g.getTs())

                }

            })
    };

    ret.login = function (req) {
        var req = $.extend({}, req); //默认参数
        $.when(commonser.login(req))
            .done(function (resp) {
                if (resp.success) {

                    $.when(commonser.getUserInfo({"userName":resp.result.userName}))
                        .done(function (resp) {
                            if (resp.success) {
                                var ui=resp.result;
                                g.setCookie("mobile",ui.mobile);
                                g.setCookie("userName",ui.userAgent);
                                g.setCookie("gender",ui.gender);
                                g.setCookie("cardType",ui.cardType);
                                g.setCookie("cardNo",ui.cardNo);
                                g.setCookie("email",ui.email);
                                g.setCookie("l",1);

                                g.go("/index.html");

                            }
                            else {
                                layer.alert(resp.error);
                            }
                        })
                }
                else {
                    $(".wt-login-error").text(resp.error);
                    $(".wt-captcha-img").attr("src", "/login/api/captchaImage/144/31/loginCaptchaImage/0?v=" + g.getTs())
                }

            })
    };

    ret.logout = function (req) {
        var req = $.extend({}, req); //默认参数

        $.when(commonser.logout(req))
            .done(function (resp) {
                if (resp.success) {
           g.clearCookies();
          g.go("/index.html");
                }
            })
    };


    ret.sendSms = function (req) {
        var req = $.extend({}, req); //默认参数

        commonser.sendSms(req);
    };



    ret.getBackPwd = function (req) {
        var req = $.extend({}, {"mobile":req.getBackPwd_mobile,"webCaptcha":req.webCaptcha}); //默认参数
        $.when(commonser.resetPwd(req))
            .done(function (resp) {
                if (resp.success) {
                    // layer.alert(resp.success);
                    layer.alert(resp.result,function() {
                        g.go("/login.html");
                    });
                
                }else {
                    layer.alert(resp.error);
                }

            })
    };

    var resetPw=$(".wt-resetPwd-form").validate({
        rules: {
            oldPassword: {
                chrnumrule:true
            },
            password: {
                chrnumrule:true
            },
            cpassword: {
                chrnumrule:true,
                equalTo: "#password"
            }
        },
        messages: {
            oldPassword: {
                required: "请输入旧密码",
                minlength: "密码长度不能小于 6 个字母"
            },
            password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字母"
            },
            cpassword: {
                required: "请输入确认密码",
                minlength: "密码长度不能小于 6 个字母",
                equalTo: "两次密码输入不一致"
            },

        },
        errorPlacement:function(error,element) {
            $(element).closest("form").find("div[for='"+element.attr("id")+"']").append(error)
        },
        submitHandler:function(form) {
            var regdata = $(".wt-resetPwd-form").form2Json();
            regdata.userName=g.getCookie("mobile");
            delete regdata.cpassword;
            ret.webUserPasswordModify(regdata)
        }
    });



    ret.webUserPasswordModify=function(regdata){
        var req = $.extend({}, regdata); //默认参数
        $.when(commonser.webUserPasswordModify(req))
            .done(function (resp) {
                if (resp.success) {
                    layer.alert(resp.result);
                    resetPw.resetForm();
                }
                else
                {
                    layer.alert(resp.error);
                }
            })
    }

    return ret;
})(jQuery, commonser, g);
